package com.clubs.coding.Controller;

import com.clubs.coding.Service.NewsService;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.common.Constant;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.dao.NewsMapper;
import com.clubs.coding.model.pojo.News;
import com.clubs.coding.model.request.NewsReq;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/admin")

public class NewsAdminController {
@Autowired
    NewsService newsService;
@Autowired
    NewsMapper newsMapper;

    @PostMapping("/upload/text")
public ApiRestResponse adminbulletin(@RequestBody NewsReq newsReq){
    newsService.adminbulletin(newsReq);
    return ApiRestResponse.success();
}
    @PostMapping("/upload/file")
    public ApiRestResponse upload(HttpServletRequest httpServletRequest,
                                  @RequestParam("file") MultipartFile file) {
        //拿到原来的名称
        String fileName = file.getOriginalFilename();
        //拿到图片的后缀
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名称UUID
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid.toString() + suffixName;
        //创建文件
        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
        if (!fileDirectory.exists()) {
            if (!fileDirectory.mkdir()) {
                throw new ImoocMallException(ImoocMallExceptionEnum.MKDIR_FAILED);
            }
        }
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
           return ApiRestResponse
                   .success(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/"
                          + newFileName);

        } catch (URISyntaxException e) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.UPLOAD_FAILED);
        }
    }
    //过滤url
    private URI getHost(URI uri) {
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                    null, null, null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }
    /**
     * 修改文章是否在首页显示和是否显示
     * **/
    @GetMapping("/alter")
    public ApiRestResponse adminalter(Integer newsId,Integer show,Integer status){
        newsService.adminalter(newsId, show, status);
        return ApiRestResponse.success();
    }

    //信息列表
    @PostMapping ("/Getnews")
    public ApiRestResponse news(@RequestParam Integer pageNum,@RequestParam Integer pageSize, Integer type){
        PageInfo news = newsService.news(pageNum, pageSize, type);
        return ApiRestResponse.success(news);
    }
    //删除知识的办法
    @PostMapping("/news/delect")
    public ApiRestResponse de(Integer id){
        newsMapper.deleteByPrimaryKey(id);
        return ApiRestResponse.success();
    }
    @PostMapping("/upload/news")
    public ApiRestResponse updatenews(@RequestBody NewsReq newsReq){
        newsService.updatenews(newsReq);
        return ApiRestResponse.success();
    }
}
